package com.sunfeng.chap1;

import java.nio.CharBuffer;

/**
 * @Author : 孙峰
 * @Description:
 * @Date : 2021/10/6  11:04
 */
public class Test11_2 {
    public static void main(String[] args) {
        CharBuffer charBuffer = CharBuffer.allocate(20);

        System.out.println("A position= " + charBuffer.position() + "  limit =" + charBuffer.limit());

        charBuffer.put("我是中国人，我在中华人民共和国");

        System.out.println("B position= " + charBuffer.position() + "  limit =" + charBuffer.limit());

        charBuffer.position(0);
        System.out.println("C position= " + charBuffer.position() + "  limit =" + charBuffer.limit());
        // 使用buffer.get() 方法是 position
        for (int i = 0; i < charBuffer.limit(); i++) {
            System.out.println(charBuffer.get());
        }

        System.out.println("D position= " + charBuffer.position() + "  limit =" + charBuffer.limit());

        charBuffer.clear();

        charBuffer.put("你好我是中国人,我爱中国！！");

        System.out.println("E position= " + charBuffer.position() + "  limit =" + charBuffer.limit());
        // 使用flip()
        //   remaining 是当前位置到限值区域的大小   limit的 值一开始是20 现在调用了flip() 现在的值为13
        charBuffer.flip();
        System.out.println("F position= " + charBuffer.position() + "  limit =" + charBuffer.limit());
        for (int i = 0; i < charBuffer.limit(); i++) {
            System.out.println(charBuffer.get());
        }

    }
}
